home *** CD-ROM | disk | FTP | other *** search
/ Univers Mac Interactif 53 / Univers Mac Interactif - Issue 53.iso / UNIVERS MAC 53 / Hypercard / Sans-Faute⁄Grammaire® / Sans-Faute⁄Grammaire ƒ / Exemples Pascal et C / Pascal / SF⁄G et AppleEvent.p < prev   
Text File  |  1995-07-21  |  18KB  |  697 lines

  1. program SFGAppleEvent;
  2.  
  3. (* Works only with the version 2.02, 2.03 or later version of Sans-Faute/Grammaire *)
  4.  
  5.     uses
  6.         AppleTalk, Processes, PPCToolBox, EPPC, Notification, AppleEvents, AERegistry;
  7.  
  8.     const
  9.         appleID = 128;
  10.         fileID = 129;
  11.         editID = 130;
  12.         sfgID = 131;
  13.  
  14.         AboutAlertID = 128;
  15.         windowID = 128;
  16.         CheckDialogID = 129;
  17.  
  18.         MenuBarID = 128;
  19.  
  20.         aboutCommand = 1;
  21.  
  22.         newCommand = 1;
  23.         quitCommand = 2;
  24.  
  25.         cutCommand = 3;
  26.         copyCommand = 4;
  27.         pasteCommand = 5;
  28.         clearCommand = 6;
  29.  
  30.         checkCommand = 1;
  31.  
  32.         IbeamCursorID = 132;
  33.  
  34.         SFGsignature = 'SFGr';
  35.  
  36.  
  37.     var
  38.         dragRect, TheSizeRect: Rect;
  39.         doneFlag: Boolean;
  40.         theWindow: WindowPtr;
  41.         theTEHdl: TEHandle;
  42.         IbeamCursor: Cursor;
  43.  
  44.         checkdoneFlag: boolean;
  45.         SFGAevtReturnID: longint;
  46.         AEcodeErr: OSErr;
  47.  
  48. (* Setup the menu bar *)
  49.     procedure SetUpMenus;
  50.  
  51.         var
  52.             menuBar: Handle;
  53.             Applemenu: MenuHandle;
  54.  
  55.     begin
  56.         menuBar := GetNewMBar(MenuBarID);
  57.         SetMenuBar(menuBar);
  58.         Applemenu := GetMHandle(appleID);
  59.         AddResMenu(Applemenu, 'DRVR');
  60.         DrawMenuBar;
  61.     end;
  62.  
  63. (* Create a text window with a styled TEHandle *)
  64.     procedure CreateNewTextWindow;
  65.  
  66.         var
  67.             txRect: Rect;
  68.  
  69.     begin
  70.         theWindow := GetNewWindow(windowID, nil, WindowPtr(-1));
  71.         SetPort(theWindow);
  72.         txRect := theWindow^.portRect;
  73.         txRect.right := txRect.right - 16;
  74.         txRect.bottom := txRect.bottom - 16;
  75.         InsetRect(txRect, 4, 4);
  76.         theTEHdl := TEStylNew(txRect, txRect);
  77.         DisableItem(GetMHandle(fileID), newCommand);
  78.         EnableItem(GetMHandle(editID), 0);
  79.         EnableItem(GetMHandle(sfgID), 0);
  80.         DrawMenuBar;
  81.         ShowWindow(theWindow);
  82.     end;
  83.  
  84. (* Dispose the text window *)
  85.     procedure DisposeTextWindow;
  86.  
  87.     begin
  88.         TEDispose(theTEHdl);
  89.         DisposeWindow(theWindow);
  90.         theWindow := nil;
  91.         theTEHdl := nil;
  92.         EnableItem(GetMHandle(fileID), newCommand);
  93.         DisableItem(GetMHandle(editID), 0);
  94.         DisableItem(GetMHandle(sfgID), 0);
  95.         DrawMenuBar;
  96.     end;
  97.  
  98. (* Treat ActivateEvt, UpdateEvt and osEvt of the main Event Loop *)
  99.     procedure TreatActivateUpdateOSEvent (theEvent: EventRecord);
  100.  
  101.         var
  102.             oldPort: GrafPtr;
  103.  
  104.     begin
  105.         case theEvent.what of
  106.             osEvt: 
  107.                 begin
  108.                     GetPort(oldPort);
  109.                     SetPort(theWindow);
  110.                     if (band(theEvent.message, suspendResumeMessage) = resumeFlag) & (FrontWindow = theWindow) then
  111.                         TEActivate(theTEHdl)
  112.                     else
  113.                         TEDeactivate(theTEHdl);
  114.                     SetPort(oldPort);
  115.                 end;
  116.             activateEvt: 
  117.                 if WindowPtr(theEvent.message) = theWindow then
  118.                     begin
  119.                         GetPort(oldPort);
  120.                         SetPort(theWindow);
  121.                         if band(theEvent.modifiers, activeFlag) <> 0 then
  122.                             TEActivate(theTEHdl)
  123.                         else
  124.                             TEDeactivate(theTEHdl);
  125.                         SetPort(oldPort);
  126.                     end;
  127.             updateEvt: 
  128.                 if WindowPtr(theEvent.message) = theWindow then
  129.                     begin
  130.                         BeginUpdate(theWindow);
  131.                         GetPort(oldPort);
  132.                         SetPort(theWindow);
  133.                         EraseRect(theWindow^.portRect);
  134.                         TEUpdate(theWindow^.portRect, theTEHdl);
  135.                         DrawGrowIcon(theWindow);
  136.                         SetPort(oldPort);
  137.                         EndUpdate(theWindow);
  138.                     end;
  139.         end;
  140.     end;
  141.  
  142. (* Routine to launch the Sans-Faute/Grammaire application*)
  143. (* based on the desktop database and the process manager *)
  144.     function launchSFG: OSErr;
  145.  
  146.         var
  147.             process: ProcessSerialNumber;
  148.             InfoRec: ProcessInfoRec;
  149.             DeskTopPB: DTPBRec;
  150.             volPB: HParamBlockRec;
  151.             SFGFound: Boolean;
  152.             SFGFSSpec: FSSpec;
  153.             err: OSErr;
  154.             theLaunchParams: LaunchParamBlockRec;
  155.  
  156.     begin
  157.         process.highLongOfPSN := 0;
  158.         process.lowLongOfPSN := kNoProcess;
  159.         InfoRec.processInfoLength := sizeof(ProcessInfoRec);
  160.         InfoRec.processName := nil;
  161.         InfoRec.processAppSpec := nil;
  162.         SFGFound := false;
  163.         while not SFGFound & (GetNextProcess(process) = noErr) do
  164.             if GetProcessInformation(process, InfoRec) = noErr then
  165.                 if (InfoRec.processType = longint('APPL')) and (InfoRec.processSignature = SFGsignature) then
  166.                     SFGFound := true;
  167.  
  168.         if SFGFound then
  169.             begin
  170.                 launchSFG := noErr;
  171.                 exit(launchSFG);
  172.             end;
  173.  
  174.         volPB.ioNamePtr := nil;
  175.         volPB.ioVolIndex := 1;
  176.         SFGFound := false;
  177.         while not SFGFound & (PBHGetVInfo(@volPB, false) = noErr) do
  178.             with DeskTopPB do
  179.                 begin
  180.                     ioNamePtr := nil;
  181.                     ioVRefnum := volPB.ioVRefnum;
  182.                     if PBDTGetPath(@DeskTopPB) = noErr then
  183.                         begin
  184.                             ioNamePtr := @SFGFSSpec.name;
  185.                             ioIndex := 0;
  186.                             ioFileCreator := SFGsignature;
  187.                             if PBDTGetAPPL(@DeskTopPB, false) = noErr then
  188.                                 begin
  189.                                     SFGFSSpec.vRefnum := volPB.ioVRefnum;
  190.                                     SFGFSSpec.parID := ioAPPLParID;
  191.                                     SFGFound := true;
  192.                                 end;
  193.                         end;
  194.                     volPB.ioVolIndex := volPB.ioVolIndex + 1;
  195.                 end;
  196.         if not SFGFound then
  197.             begin
  198.                 launchSFG := fnfErr;
  199.                 exit(launchSFG);
  200.             end;
  201.  
  202.         with theLaunchParams do
  203.             begin
  204.                 launchBlockID := extendedBlock;
  205.                 launchEPBLength := extendedBlockLen;
  206.                 launchFileFlags := 0;
  207.                 launchControlFlags := launchContinue + launchNoFileFlags;
  208.                 launchAppSpec := @SFGFSSpec;
  209.                 launchAppParameters := nil;
  210.             end;
  211.         err := LaunchApplication(@theLaunchParams);
  212.         launchSFG := err;
  213.  
  214.     end;
  215.  
  216. (* Routine to put Sans-Faute/Grammaire in front*)
  217.     procedure PutSFGInFront;
  218.  
  219.         var
  220.             process: ProcessSerialNumber;
  221.             InfoRec: ProcessInfoRec;
  222.             SFGFound: boolean;
  223.             err: OSErr;
  224.  
  225.     begin
  226.         process.highLongOfPSN := 0;
  227.         process.lowLongOfPSN := kNoProcess;
  228.         InfoRec.processInfoLength := sizeof(ProcessInfoRec);
  229.         InfoRec.processName := nil;
  230.         InfoRec.processAppSpec := nil;
  231.         SFGFound := false;
  232.         while not SFGFound & (GetNextProcess(process) = noErr) do
  233.             if GetProcessInformation(process, InfoRec) = noErr then
  234.                 if (InfoRec.processType = longint('APPL')) and (InfoRec.processSignature = SFGsignature) then
  235.                     begin
  236.                         SFGFound := true;
  237.                         err := SetFrontProcess(process);
  238.                     end;
  239.     end;
  240.  
  241. (* Routine used to initialize a AEDesc *)
  242.     procedure MyInitDesc (var desc: AEDesc);
  243.  
  244.     begin
  245.         desc.descriptorType := typeNull;
  246.         desc.dataHandle := nil;
  247.     end;
  248.  
  249. (* Routine used to dispose a AEDesc *)
  250.     procedure MyDisposeDesc (var desc: AEDesc);
  251.  
  252.         var
  253.             err: OSErr;
  254.  
  255.     begin
  256.         if desc.dataHandle <> nil then
  257.             begin
  258.                 err := AEDisposeDesc(desc);
  259.                 MyInitDesc(desc);
  260.             end;
  261.     end;
  262.  
  263. (* AppleEvent Handler used to receive the answer from Sans-Faute/Grammaire *)
  264.     function SFGAnswerHandler (theAppleEvent: AppleEvent; reply: AppleEvent; HandlerRefCon: longint): OSErr;
  265.  
  266.         var
  267.             result, textDesc, StyleDesc: AEDesc;
  268.             tempRect: Rect;
  269.             err: OSErr;
  270.             AevtReturnID: longint;
  271.             actualSize: Size;
  272.             actualTypeCode: DescType;
  273.  
  274.     begin
  275.         err := AEGetAttributePtr(theAppleEvent, keyReturnIDAttr, typeLongInteger, actualTypeCode, @AevtReturnID, sizeof(longint), actualSize);
  276.         if (err = noErr) & (SFGAevtReturnID = AevtReturnID) then
  277.             begin
  278.                 checkdoneFlag := true;
  279.                 err := AEGetParamPtr(theAppleEvent, keyErrorNumber, typeShortInteger, actualTypeCode, @AEcodeErr, SizeOf(OSErr), actualSize);
  280.                 if err = NoErr then
  281.                     begin
  282.                         if AEcodeErr = noErr then
  283.                             begin
  284.                                 MyInitDesc(result);
  285.                                 MyInitDesc(textDesc);
  286.                                 MyInitDesc(StyleDesc);
  287.                                 err := AEGetParamDesc(theAppleEvent, keyDirectObject, typeAERecord, result);
  288.                                 if err = noErr then
  289.                                     err := AEGetKeyDesc(result, keyAEText, typeChar, textDesc);
  290.                                 if err = noErr then
  291.                                     err := AEGetKeyDesc(result, keyAEStyles, typeScrapStyles, StyleDesc);
  292.                                 if err = noErr then
  293.                                     begin
  294.                                         TESetSelect(0, maxint, theTEHdl);
  295.                                         TEDelete(theTEHdl);
  296.                                         TEStylInsert(textDesc.dataHandle^, GetHandleSize(textDesc.dataHandle), StScrpHandle(StyleDesc.dataHandle), theTEHdl);
  297.                                         tempRect := theTEHdl^^.viewRect;
  298.                                         InvalRect(tempRect);
  299.                                     end
  300.                                 else
  301.                                     AEcodeErr := err;
  302.                                 MyDisposeDesc(result);
  303.                                 MyDisposeDesc(textDesc);
  304.                                 MyDisposeDesc(StyleDesc);
  305.                             end
  306.                     end
  307.                 else
  308.                     AEcodeErr := err;
  309.             end;
  310.         SFGAnswerHandler := noErr;
  311.     end;
  312.  
  313. (* Routine used to check the grammar of the styled TEHandle with Sans-Faute/Grammaire *)
  314.     procedure checkSFG;
  315.  
  316. (* Build a AEDesc with styled text into it*)
  317.         function BuildStyledTextDesc (var resultDesc: AEDesc): OSErr;
  318.             var
  319.                 StyleListDesc: AEDesc;
  320.                 theStScrpHandle: StScrpHandle;
  321.                 err: OSErr;
  322.                 TheText: Handle;
  323.                 oldSelStart, oldSelEnd: integer;
  324.  
  325.         begin
  326.             MyInitDesc(resultDesc);
  327.             MyInitDesc(StyleListDesc);
  328.  
  329.             err := AECreateList(nil, 0, true, StyleListDesc);
  330.             TheText := handle(TEGetText(theTEHdl));
  331.             HLock(TheText);
  332.             if err = noErr then
  333.                 err := AEPutKeyPtr(StyleListDesc, keyAEText, typeChar, TheText^, GetHandleSize(TheText));
  334.             HUnlock(TheText);
  335.  
  336.             TEDeactivate(theTEHdl);
  337.             with theTEHdl^^ do
  338.                 begin
  339.                     oldSelStart := SelStart;
  340.                     oldSelEnd := SelEnd;
  341.                 end;
  342.             TESetSelect(0, maxint, theTEHdl);
  343.             theStScrpHandle := GetStylScrap(theTEHdl);
  344.             TESetSelect(oldSelStart, oldSelEnd, theTEHdl);
  345.  
  346.             if theStScrpHandle <> nil then
  347.                 begin
  348.                     HLock(Handle(theStScrpHandle));
  349.                     if err = noErr then
  350.                         err := AEPutKeyPtr(StyleListDesc, keyAEStyles, typeScrapStyles, Ptr(theStScrpHandle^), GetHandleSize(Handle(theStScrpHandle)));
  351.                     disposhandle(Handle(theStScrpHandle));
  352.                 end
  353.             else
  354.                 err := AEPutKeyPtr(StyleListDesc, keyAEStyles, typeScrapStyles, nil, 0);
  355.             if err = noErr then
  356.                 err := AECoerceDesc(StyleListDesc, typeStyledText, resultDesc);
  357.             MyDisposeDesc(StyleListDesc);
  358.  
  359.             BuildStyledTextDesc := err;
  360.         end;
  361.  
  362.  
  363. (* Send the AppleEvent to Sans-Faute/Grammaire *)
  364. (* Please note that the AppleEvent used in an extension of the Word Services Suite specific to Sans-Faute/Grammaire *)
  365.         function SendAECheckToSFG: OSErr;
  366.  
  367.             var
  368.                 err: OSerr;
  369.                 targetSignature: OSType;
  370.                 target, directParamDesc: AEDesc;
  371.                 evt, reply: AppleEvent;
  372.                 tempRect: Rect;
  373.                 actualTypeCode: DescType;
  374.                 actualSize: Size;
  375.  
  376.         begin
  377.             err := launchSFG;
  378.             if err <> noErr then
  379.                 begin
  380.                     SendAECheckToSFG := err;
  381.                     exit(SendAECheckToSFG);
  382.                 end;
  383.             MyInitDesc(evt);
  384.             MyInitDesc(reply);
  385.             MyInitDesc(directParamDesc);
  386.             MyInitDesc(target);
  387.             targetSignature := SFGsignature;
  388.             err := AECreateDesc(typeApplSignature, @targetSignature, sizeof(targetSignature), target);
  389.             if err = noErr then
  390.                 err := AECreateAppleEvent('WSrv', 'Btch', target, kAutoGenerateReturnID, kAnyTransactionID, evt);
  391.             if err = noErr then
  392.                 err := AEGetAttributePtr(evt, keyReturnIDAttr, typeLongInteger, actualTypeCode, @SFGAevtReturnID, sizeof(longint), actualSize);
  393.             if err = noErr then
  394.                 err := BuildStyledTextDesc(directParamDesc);
  395.             if err = noErr then
  396.                 err := AEPutParamDesc(evt, keyDirectObject, directParamDesc);
  397.             if err = noErr then
  398.                 err := AESend(evt, reply, kAEQueueReply + kAEAlwaysInteract + kAECanSwitchLayer, kAENormalPriority, kNoTimeOut, nil, nil);
  399.             MyDisposeDesc(evt);
  400.             MyDisposeDesc(reply);
  401.             MyDisposeDesc(directParamDesc);
  402.             MyDisposeDesc(target);
  403.             SendAECheckToSFG := err;
  404.         end;
  405.  
  406.         var
  407.             checkDialog: DialogPtr;
  408.  
  409. (* Treat ActivateEvt , UpdateEvt and osEvt of the Dialog Event Loop *)
  410.         procedure TreatActivateUpdateOSDialogEvent (theEvent: EventRecord);
  411.  
  412.             var
  413.                 oldPort: GrafPtr;
  414.  
  415.         begin
  416.             if WindowPtr(theEvent.message) = checkDialog then
  417.                 case theEvent.what of
  418.                     updateEvt: 
  419.                         begin
  420.                             BeginUpdate(checkDialog);
  421.                             GetPort(oldPort);
  422.                             SetPort(checkDialog);
  423.                             UpdtDialog(checkDialog, checkDialog^.visRgn);
  424.                             SetPort(oldPort);
  425.                             EndUpdate(checkDialog);
  426.                         end;
  427.                 end
  428.             else
  429.                 TreatActivateUpdateOSEvent(theEvent);
  430.         end;
  431.  
  432. (* Enable or Disable the Menu Bar *)
  433.         procedure EnableDisableMenuBar (Enable: boolean);
  434.  
  435.  
  436.         begin
  437.             if Enable then
  438.                 begin
  439.                     EnableItem(GetMHandle(appleID), 0);
  440.                     EnableItem(GetMHandle(fileID), 0);
  441.                     EnableItem(GetMHandle(editID), 0);
  442.                     EnableItem(GetMHandle(sfgID), 0);
  443.                 end
  444.             else
  445.                 begin
  446.                     DisableItem(GetMHandle(appleID), 0);
  447.                     DisableItem(GetMHandle(fileID), 0);
  448.                     DisableItem(GetMHandle(editID), 0);
  449.                     DisableItem(GetMHandle(sfgID), 0);
  450.                 end;
  451.             DrawMenuBar;
  452.         end;
  453.  
  454.         var
  455.             whichWindow: WindowPtr;
  456.             theEvent: EventRecord;
  457.             err: OSerr;
  458.             ignore: longint;
  459.             oldPort: GrafPtr;
  460.             MousePt: Point;
  461.             whichControl: ControlHandle;
  462.             i: integer;
  463.             shouldShowAlert: boolean;
  464.  
  465.     begin
  466.         GetPort(oldPort);
  467.         checkDialog := GetNewDialog(CheckDialogID, nil, WindowPtr(-1));
  468.         SetPort(checkDialog);
  469.         ShowWindow(checkDialog);
  470.         checkdoneFlag := false;
  471.         shouldShowAlert := false;
  472.         EnableDisableMenuBar(false);
  473.         err := AEInstallEventHandler(kCoreEventClass, kAEAnswer, @SFGAnswerHandler, 0, false);
  474.         err := SendAECheckToSFG;
  475.         if err <> noErr then
  476.             shouldShowAlert := true
  477.         else
  478.             repeat
  479.                 if WaitNextEvent(everyEvent, theEvent, 30, nil) then
  480.                     case theEvent.what of
  481.                         ActivateEvt, UpdateEvt, osEvt: 
  482.                             TreatActivateUpdateOSDialogEvent(theEvent);
  483.                         kHighLevelEvent: 
  484.                             err := AEProcessAppleEvent(theEvent);
  485.                         mouseDown: 
  486.                             case FindWindow(theEvent.where, whichWindow) of
  487.                                 inSysWindow: 
  488.                                     SystemClick(theEvent, whichWindow);
  489.                                 inDrag: 
  490.                                     if whichWindow = checkDialog then
  491.                                         DragWindow(whichWindow, theEvent.where, dragRect);
  492.                                 inMenuBar: 
  493.                                     ignore := MenuSelect(theEvent.where);
  494.                                 inContent: 
  495.                                     if whichWindow = checkDialog then
  496.                                         begin
  497.                                             MousePt := theEvent.where;
  498.                                             GlobalToLocal(MousePt);
  499.                                             if (FindControl(MousePt, checkDialog, whichControl) = inButton) & (TrackControl(whichControl, MousePt, nil) = inButton) then
  500.                                                 PutSFGInFront;
  501.                                         end;
  502.                                 otherwise
  503.                                     SysBeep(1);
  504.                             end;
  505.                     end;
  506.             until checkDoneFlag;
  507.         err := AERemoveEventHandler(kCoreEventClass, kAEAnswer, @SFGAnswerHandler, false);
  508.         EnableDisableMenuBar(true);
  509.         DisposeDialog(checkDialog);
  510.         SetPort(oldPort);
  511.         if (AEcodeErr <> noErr) and (AEcodeErr <> errAEWaitCanceled) then
  512.             shouldShowAlert := true;
  513.         if shouldShowAlert then
  514.             i := Alert(130, nil);
  515.     end;
  516.  
  517. (* Used to treat a command from the menu bar *)
  518.  
  519.     procedure DoMenuCommand (MenuResult: longint);
  520.  
  521.         var
  522.             theItem, theMenu: integer;
  523.             temp: integer;
  524.             DAName: str255;
  525.             err: OSErr;
  526.             theStScrpHandle: StScrpHandle;
  527.  
  528.     begin
  529.         theItem := LoWrd(menuResult);
  530.         theMenu := HiWrd(menuResult);
  531.         case theMenu of
  532.             appleID: 
  533.                 if theItem = aboutCommand then
  534.                     begin
  535.                         temp := Alert(AboutAlertID, nil);
  536.                     end
  537.                 else
  538.                     begin
  539.                         GetItem(GetMHandle(appleID), theItem, DAName);
  540.                         temp := OpenDeskAcc(DAName);
  541.                     end;
  542.             fileID: 
  543.                 case theItem of
  544.                     newCommand: 
  545.                         CreateNewTextWindow;
  546.                     quitCommand: 
  547.                         doneFlag := true;
  548.                 end;
  549.             editID: 
  550.                 case theItem of
  551.                     cutCommand: 
  552.                         begin
  553.                             TECut(theTEHdl);
  554.                             if ZeroScrap = noErr then
  555.                                 if TEToScrap = noErr then
  556.                                     begin
  557.                                         theStScrpHandle := GetStylScrap(theTEHdl);
  558.                                         HLock(Handle(theStScrpHandle));
  559.                                         err := PutScrap(GetHandleSize(Handle(theStScrpHandle)), 'styl', Handle(theStScrpHandle)^);
  560.                                         HUnlock(Handle(theStScrpHandle));
  561.                                     end;
  562.                         end;
  563.                     copyCommand: 
  564.                         begin
  565.                             TECopy(theTEHdl);
  566.                             if ZeroScrap = noErr then
  567.                                 if TEToScrap = noErr then
  568.                                     begin
  569.                                         theStScrpHandle := GetStylScrap(theTEHdl);
  570.                                         HLock(Handle(theStScrpHandle));
  571.                                         err := PutScrap(GetHandleSize(Handle(theStScrpHandle)), 'styl', Handle(theStScrpHandle)^);
  572.                                         HUnlock(Handle(theStScrpHandle));
  573.                                     end;
  574.                         end;
  575.                     pasteCommand: 
  576.                         if TEFromScrap = noErr then
  577.                             TEStylPaste(theTEHdl);
  578.                     clearCommand: 
  579.                         TEDelete(theTEHdl);
  580.                 end;
  581.             sfgID: 
  582.                 if theItem = checkCommand then
  583.                     begin
  584.                         HiliteMenu(0);
  585.                         checkSFG;
  586.                     end;
  587.         end;
  588.         HiliteMenu(0);
  589.     end;
  590.  
  591. (* Initialize the ToolBox *)
  592.  
  593.     procedure InitToolbox;
  594.  
  595.     begin
  596.         MaxApplZone;
  597.         InitGraf(@thePort);
  598.         InitFonts;
  599.         InitWindows;
  600.         InitMenus;
  601.         TEInit;
  602.         InitDialogs(nil);
  603.         InitCursor;
  604.         IbeamCursor := getcursor(IbeamCursorID)^^;
  605.     end;
  606.  
  607.  
  608.     var
  609.         theEvent: EventRecord;
  610.         whichWindow: WindowPtr;
  611.         resultGrow: longint;
  612.         newH, newV: integer;
  613.         newtxRect, tempRect: Rect;
  614.         MousePt: Point;
  615.         theChar: char;
  616.  
  617.  
  618. begin
  619. (* The next line is only for THINK Pascal *)
  620. {$I-}
  621.     InitToolbox;
  622.     SetUpMenus;
  623.     with ScreenBits.bounds do
  624.         begin
  625.             setRect(dragRect, 4, 24, right - 4, bottom - 4);
  626.             setRect(TheSizeRect, 40, 40, right - left - 40, bottom - top - 40);
  627.         end;
  628.     doneFlag := false;
  629.     CreateNewTextWindow;
  630.     repeat
  631.         GetMouse(MousePt);
  632.         if theTEHdl <> nil then
  633.             begin
  634.                 TEIdle(theTEHdl);
  635.                 tempRect := theTEHdl^^.viewRect;
  636.                 if PtInrect(mousePt, tempRect) then
  637.                     SetCursor(IBeamCursor)
  638.                 else
  639.                     InitCursor;
  640.             end
  641.         else
  642.             initcursor;
  643.         if WaitNextEvent(everyEvent, theEvent, 30, nil) then
  644.             case theEvent.what of
  645.                 mouseDown: 
  646.                     case FindWindow(theEvent.where, whichWindow) of
  647.                         inSysWindow: 
  648.                             SystemClick(theEvent, whichWindow);
  649.                         inMenuBar: 
  650.                             DoMenuCommand(MenuSelect(theEvent.where));
  651.                         inDrag: 
  652.                             DragWindow(whichWindow, theEvent.where, dragRect);
  653.                         inGrow: 
  654.                             if whichWindow = theWindow then
  655.                                 begin
  656.                                     resultGrow := GrowWindow(theWindow, theEvent.where, TheSizeRect);
  657.                                     newH := LoWrd(resultGrow);
  658.                                     newV := HiWrd(resultGrow);
  659.                                     SizeWindow(theWindow, newH, newV, TRUE);
  660.                                     newtxRect := theWindow^.portRect;
  661.                                     newtxRect.right := newtxRect.right - 16;
  662.                                     newtxRect.bottom := newtxRect.bottom - 16;
  663.                                     InsetRect(newtxRect, 4, 4);
  664.                                     theTEHdl^^.destRect := newtxRect;
  665.                                     theTEHdl^^.viewRect := newtxRect;
  666.                                     TECalText(theTEHdl);
  667.                                     InvalRect(theWindow^.portRect);
  668.                                 end;
  669.                         inGoAway: 
  670.                             if whichWindow = theWindow then
  671.                                 if TrackGoAway(theWindow, theEvent.where) then
  672.                                     DisposeTextWindow;
  673.                         inContent: 
  674.                             if whichWindow <> FrontWindow then
  675.                                 SelectWindow(whichWindow)
  676.                             else if (whichWindow = theWindow) & (theTEHdl <> nil) then
  677.                                 begin
  678.                                     MousePt := theEvent.where;
  679.                                     GlobalToLocal(MousePt);
  680.                                     tempRect := theTEHdl^^.viewRect;
  681.                                     if PtInrect(mousePt, tempRect) then
  682.                                         TEClick(mousePt, band(theEvent.modifiers, shiftKey) <> 0, theTEHdl);
  683.                                 end;
  684.                     end;
  685.                 keyDown, autoKey: 
  686.                     begin
  687.                         theChar := chr(band(theEvent.message, charCodeMask));
  688.                         if band(theEvent.modifiers, cmdKey) <> 0 then
  689.                             DoMenuCommand(MenuKey(theChar))
  690.                         else if theTEHdl <> nil then
  691.                             TEKey(theChar, theTEHdl);
  692.                     end;
  693.                 activateEvt, updateEvt, osEvt: 
  694.                     TreatActivateUpdateOSEvent(theEvent);
  695.             end;
  696.     until doneFlag;
  697. end.